import { Suspense, lazy } from "react"
import { createHashRouter, Navigate } from "react-router-dom"
import { Spin } from "antd"
import Login from "@/pages/login"
import Home from "@/pages/home"
import UserList from "@/pages/users/user-list"
import UserRights from "@/pages/users/user-rights"
import MySynergy from "@/pages/synergy/my-synergy"
import TodoColla from "@/pages/synergy/todo-colla"
import MyDaily from "@/pages/weekly/my-daily"
import StaffDaily from "@/pages/weekly/staff-daily"
import CustomerStatistics from "@/pages/statement/customer-statistics"
// 全局路由
function globalRoute() {
  // 二级路由框架页面采用懒加载方式
  const Entry = lazy(() => import("@/pages/entry"))
  // 使用 createBrowserRouter 或 createHashRouter 创建路由实例
  return createHashRouter([
    {
      // 精确匹配"/login"，跳转Login页面
      path: "/login",
      element: <Login />,
    },
    {
      // 未匹配"/login"，则进入到Entry页面
      path: "/",
      element: (
        // 懒加载过程中先使用Spin组件占位
        <Suspense fallback={<Spin />}>
          <Entry />
        </Suspense>
      ),
      // 定义Entry二级路由
      children: [
        {
          // 精确匹配"/home"，跳转Home页面
          path: "/home",
          element: <Home />,
        },
        {
          // 精确匹配"/user-list"，跳转user-list页面
          path: "/user-list",
          element: <UserList />,
        },
        {
          path: "/user-rights",
          element: <UserRights />,
        },
        {
          path: "/my-synergy",
          element: <MySynergy />,
        },
        {
          path: "/todo-colla",
          element: <TodoColla />,
        },
        {
          path: "/my-daily",
          element: <MyDaily />,
        },
        {
          path: "/staff-daily",
          element: <StaffDaily />,
        },
        {
          path: "/customer-statistics",
          element: <CustomerStatistics />,
        },
        {
          // 如果URL没有"#路由"，跳转Home页面
          path: "/",
          element: <Navigate to="/home" />,
        },
        {
          // 未匹配，跳转Login页面
          path: "*",
          element: <Navigate to="/login" />,
        },
      ],
    },
  ])
}

const globalRouter = globalRoute()

export default globalRouter
